﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SDT.Admin.ViewModels;
using SDT.Model;
using SDT.Lib.Common;
using SDT.Lib.Paging;
using SDT.Admin.Properties;

namespace SDT.Admin.Controllers
{
    public class OnlineSupportController : Controller
    {
        //Class Name: OnlineSupport
        //Create by: Hung
        //Quản lý hỗ trợ trực tuyến

        private SDTDataContext db = new SDTDataContext();
        private OnlineSupportViewModel os = new OnlineSupportViewModel();
        [Authorize]
        public ActionResult Index(int? page)
        {
            try
            {
                if (!page.HasValue) page = 1;
                string order = UserState.getUserStateFromRequest("online.order", "order", "ID").ToString();
                string orderDir = UserState.getUserStateFromRequest("online.orderDir", "orderDir", "desc").ToString();
                int pageSize = int.Parse(UserState.getUserStateFromRequest("online.pageSize", "PageSize", 20).ToString());
                os.OnlineSupports = db.OnlineSupports.ToPagedList(page.Value - 1, pageSize);
                var listCat = Utils.StringToDictionary(Settings.Default.OnlineSupportList.ToString(), ",");
                os.ListOnlineCat = listCat;
                return View(os);
            }
            catch (System.Exception ex)
            {
                ViewData[Messages.MessageInfo] = ex.Message;
                return View();
            }
            
        }
        [Authorize]
        public ActionResult Create()
        {
            try
            {
                var listCat = Utils.StringToDictionary(Settings.Default.OnlineSupportList.ToString(), ",");
                return View(new OnlineInputModel
                {
                    SupportType = listCat
                });
            }
            catch (System.Exception ex)
            {
                ViewData[Messages.MessageInfo] = ex.Message;
                return View();
            }
        }
        [Authorize]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Create(OnlineInputModel m)
        {
            try
            {
           
                UpdateModel(m);
                m.OnlineSupport.SupportType = !string.IsNullOrEmpty(Request["SupportType"]) ? Convert.ToInt32(Request["SupportType"]) : 0;
                db.OnlineSupports.InsertOnSubmit(m.OnlineSupport);
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (System.Exception ex)
            {
                ViewData[Messages.MessageInfo] = ex.Message;
                return View();
            }
        }
        [Authorize]
        public ActionResult Edit(int id)
        {
            try
            {
                var listCat = Utils.StringToDictionary(Settings.Default.OnlineSupportList.ToString(), ",");
                var online = db.OnlineSupports.SingleOrDefault(d => d.ID == id);
                return View(new OnlineInputModel
                {
                    OnlineSupport = online,
                    SupportType = listCat
                });
            }
            catch (System.Exception ex)
            {
                ViewData[Messages.MessageInfo] = ex.Message;
                return View();
            }
        }
        [Authorize]
        [ValidateInput(false)]
        [HttpPost]
        public ActionResult Edit(int id,OnlineInputModel m)
        {
            try
            {
                m.OnlineSupport = db.OnlineSupports.SingleOrDefault(d => d.ID == id);
                UpdateModel(m);
                m.OnlineSupport.SupportType = !string.IsNullOrEmpty(Request["SupportType"]) ? Convert.ToInt32(Request["SupportType"]) : 0;            
                db.SubmitChanges();
                return RedirectToAction("Index");
            }
            catch (System.Exception ex)
            {
                ViewData[Messages.MessageInfo] = ex.Message;
                return View();
            }
        }
        [Authorize]
        public ActionResult Delete(int id)
        {
            db.OnlineSupports.DeleteOnSubmit(db.OnlineSupports.SingleOrDefault(d => d.ID == id));
            db.SubmitChanges();
            return RedirectToAction("Index");
        }
        [Authorize]
        public ActionResult Details(int id)
        {
            try
            {
                os.OnlineSupport = db.OnlineSupports.SingleOrDefault(d => d.ID == id);
                var listCat = Utils.StringToDictionary(Settings.Default.OnlineSupportList.ToString(), ",");
                os.ListOnlineCat = listCat;
                return View(os);
            }
            catch (System.Exception ex)
            {
                ViewData[Messages.MessageInfo] = ex.Message;
                return View();
            }
        }
    }
}
